package com.example.controller;

import com.example.constant.Constants;
import com.example.entity.Car;
import com.example.entity.User;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("test")
public class TestController {
    /**
     * 注入 RabbitTemplate 模板对象
     */
    @Resource
    private RabbitTemplate rabbitTemplate;


    @RequestMapping("send/{name}")
    public String send(@PathVariable("name") String name) {

        /**
         * 发送消息
         */
        String str = name + "\t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss_SSS").format(new Date());
        rabbitTemplate.convertAndSend(Constants.MY_FANOUT_EXCHANGE, "", str);
        System.out.println("消息发送成功!\t" + str);
        return "success";
    }

    @RequestMapping("user/{name}")
    public String sendUser(@PathVariable("name") String name) {

        String str = name + "\t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss_SSS").format(new Date());
        User user = new User();

        user.setUserName(str);
        user.setUserAge(18);
        user.setCar(new Car("BMW", 100));

        rabbitTemplate.convertAndSend(Constants.MY_FANOUT_EXCHANGE, "", user);
        System.out.println("用户对象发送成功!\t" + str);
        return "success";
    }


}
